﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Windows.Automation;
using Compiled_Tests.AutomationAbstractions;
using Tedds_Tool.Instructions;
using Tedds_Tool.Instructions.Actions;
using CommonInterfaces.Models;
using StandardScriptItems.SimpleControls;
using LEET.CommonInterfaces.Automation;
using LEET.CommonInterfaces.Factories;
using LEET.LEETCommon.AutomationAbstractions;
using LEET.CommonInterfaces.Models;
using LEET.LEETCommon.Model;
using System.Windows;
using LEET.Exceptions.Models;

namespace Tedds_Tool.Automatables
{
    public class Selectable : ILEETActionModule
    {
        public AutomationElementEventListener CreateListener(IObservableWidget elem, IRegistryAccessor reg)
        {
            return new EventListener(elem, reg);
        }

        public bool CanCompile(string instruction)
        {
            try
            {
                String[] parts = Instruction.ParseInstruction(instruction);
                return "ACTION".Equals(parts[0]) && TypeName.Equals(parts[1]);
            }
            catch (InvalidInstructionException)
            {
                return false;
            }
        }

        public IInstruction CreateInstruction(string instruction)
        {
            return new Select(Instruction.ParseInstruction(instruction));
        }

        

        private class EventListener : AutomationElementEventListenerBase
        {

            AutomationEventHandler handler;
            private IRegistryAccessor registry;

            public EventListener(IObservableWidget target, IRegistryAccessor reg)
            {
                this.RootTarget = target;
                this.registry = reg;
                Listening = false;
                try
                {
                    handler = new AutomationEventHandler(CatchEvent);
                    //registry.RegisterEventHandler(SelectionItemPattern.ElementSelectedEvent, RootTarget.ae, handler);
                    registry.RegisterEventHandler(SelectionItemPatternIdentifiers.ElementSelectedEvent, RootTarget.ae, handler);
                    Listening = true;
                }
                catch (ArgumentException) { }
            }

            public override void Listen(IObservableWidget widget, string property)
            {
                //In case element disappeared before it could be processed
                if (widget == null || !property.Equals("SelectionItemPatternIdentifiers.ElementSelectedEvent"))
                    return;

                //each target root element should have a HierarchyFinder
                string fullpath = "";
                if (RootTarget.ae != HierarchyFinder.Root)
                {
                    HierarchyFinder.SetUIAHierarchy(RootTarget);
                }
                fullpath = HierarchyFinder.GetPathFromHierarchy(widget);
                HierarchyFinder.SetUIAHierarchy(RootTarget);

                try
                {
                    if (widget.ae.GetCurrentPattern(SelectionItemPattern.Pattern) != null && widget.ae.GetCurrentPattern(SelectionItemPatternIdentifiers.Pattern) != null)
                        Delagate(new Event_Pair("ACTION\t| SELECT | " + fullpath), widget.ae);
                }
                catch
                {
                    Delagate(new Event_Pair("ACTION\t| SELECT | " + fullpath), widget.ae);
                }
            }

            public override void Dispose()
            {
                //registry.RemoveEventHandler(SelectionItemPattern.ElementSelectedEvent, RootTarget.ae, handler);
                registry.RemoveEventHandler(SelectionItemPatternIdentifiers.ElementSelectedEvent, RootTarget.ae, handler);
                base.Dispose();
            }

        }

        public void Initialize()
        {
        }

        public string TypeName
        {
            get { return "SELECT"; }
        }

        public FrameworkElement CreateScriptItemDetails(LEET.CommonInterfaces.Nil onUpdate, IEnumerable<string> processes, IElementState state)
        {
            return new ElementStateComboBox(state);
        }
    }
}
